#include "HelloWorldScene.h"
#include "Fish.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

//On "init" you need to initialize your instance
bool HelloWorld::init()
{
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    /* Load resource*/
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/sprite.plist");

    /* Get screen size */
    this->pSize = CCDirector::sharedDirector()->getVisibleSize();
    /* */
    //cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
    /* Load background */
    loadBackground();
    /* Load fish */
    loadFish();

    return true;
}

/* Load backdground */
void HelloWorld::loadBackground()
{
		CCSprite* bg = CCSprite::createWithSpriteFrameName("background.png");
	    bg->setAnchorPoint(ccp(0,0));
	    bg->setPosition(ccp(0,0));
	    bg->setScaleX(pSize.width/bg->getContentSize().width);
	    bg->setScaleY(pSize.height/bg->getContentSize().width);
	    //
	    this->addChild(bg);
}

/**/
void HelloWorld::loadFish()
{
	oFish = new CFish();
	oFish->setZOrder(1000);
	oFish->CCNode::setPosition(pSize.width/2, pSize.height/2);
	oFish->addComponent(new CGamePlay());
	//
	this->addChild(oFish);
}

void HelloWorld::onEnter()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
	CCLayer::onEnter();
}

bool HelloWorld::ccToucheBegan(CCSet* touches, CCEvent* event )
{
	oFish->gStatus = isTap;
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/lh_point.ogg");
	return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
